//
//  YTShareViewController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/6.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTShareViewController.h"
#import "YTShareHeaderView.h"
#import "YTShareCell.h"
#import "YTNewShareView.h"

#define kShareViewH 104

@interface YTShareViewController ()<UITableViewDataSource>
@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)YTNewShareView *shareView;
@end

@implementation YTShareViewController

// lazy shareView
- (YTNewShareView *)shareView{
    if (!_shareView) {
       
        __weak typeof(self) WeakSelf = self;
        _shareView = [YTNewShareView shareViewWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableView.frame), kScreenWidth, kShareViewH)];
        _shareView.shareList = @[@{@"imageName":@"icon_wechat",@"name":@"微信",@"type":@(UMSocialPlatformType_WechatSession)},
                                 @{@"imageName":@"icon_wechatCyc",@"name":@"朋友圈",@"type":@(UMSocialPlatformType_WechatTimeLine)}
                                    ];
        
        // 取消按钮点击
        _shareView.cancelButtonClick = ^{
            [WeakSelf dismissViewControllerAnimated:true completion:nil];
        };
        
        // 选择分享的平台点击
        _shareView.selectedSharaTypeToShare = ^(int type) {
            [WeakSelf shareWebPageToPlatformType:type image:[WeakSelf captureScrollView:WeakSelf.tableView]];
        };
    }
    return _shareView;
}

// lazy table view
- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -kStatusBarH, kScreenWidth, kScreenHeight - kShareViewH + kStatusBarH)
                                                  style:UITableViewStylePlain];
        
        _tableView.tableHeaderView = [YTShareHeaderView headerView];
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 153;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 300;//估算高度
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
        
        // 注册可重用cell
        [_tableView registerNib:[UINib nibWithNibName:@"YTShareCell" bundle:nil]
                                                forCellReuseIdentifier:@"cellID"];
    }
    return _tableView;
}


#pragma mark: - 截图的方法
- (UIImage *)captureScrollView:(UIScrollView *)scrollView{
    UIImage* image = nil;
    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, [UIScreen.mainScreen scale]);
    {
        CGPoint savedContentOffset = scrollView.contentOffset;
        CGRect savedFrame = scrollView.frame;
        scrollView.contentOffset = CGPointZero;
        scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
        
        [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
        
        scrollView.contentOffset = savedContentOffset;
        scrollView.frame = savedFrame;
    }
    UIGraphicsEndImageContext();
    
    if (image != nil) {
        return image;
    }
    return nil;
}

#pragma mark: - 第三方分享的方法
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType image:(UIImage *)image
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    [shareObject setShareImage:image];
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;

    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                
                [YTNetworkManager.shareManager requestSiginWithType:@"9"
                                                            post_id:self.model.ID
                                                         completion:^(BOOL isSucc, NSString *StatusInfo)
                {
                    if (!isSucc) {
                        [self.view showErrorWithTipStr:StatusInfo];
                        return ;
                    }
                    
                    [self.view showAwardWithTipStr:[NSString stringWithFormat:@"分享成功 +%@算力",StatusInfo]];
                }];
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);

            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
        NSLog(@"分享出错%@",error);//修改代码
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self.view addSubview:self.tableView];
    [self.view addSubview:self.shareView];
    
    self.view.backgroundColor = UIColor.randomColor;
}


#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTShareCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.model = self.model;
    return cell;
}
@end
